package com.xbongbong.paas.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.MobileFocusModeTypeEnum;
import com.xbongbong.paas.enums.MobileIndexModeEnum;
import com.xbongbong.paas.enums.PlatFormEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.CommonHelp;
import com.xbongbong.paas.log.MongoLogHelp;
import com.xbongbong.paas.log.constant.LogMemoConstant;
import com.xbongbong.paas.log.pojo.InfoArrPojo;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.pojo.MobileMenuModeSetGetPojo;
import com.xbongbong.paas.pojo.dto.ColumnSetAddDTO;
import com.xbongbong.paas.pojo.dto.ColumnSetListDTO;
import com.xbongbong.paas.pojo.dto.DelColumnSetAddDTO;
import com.xbongbong.paas.pojo.dto.DelColumnSetListDTO;
import com.xbongbong.paas.pojo.dto.OfficPreviewUrlSaveDTO;
import com.xbongbong.paas.pojo.dto.UpdateMobileFocusModeDTO;
import com.xbongbong.paas.pojo.dto.UpdateMobileModeDTO;
import com.xbongbong.paas.pojo.dto.UserConfigAddDTO;
import com.xbongbong.paas.pojo.vo.ColumnSetAddVO;
import com.xbongbong.paas.pojo.vo.ColumnSetListVO;
import com.xbongbong.paas.pojo.vo.DelColumnSetAddVO;
import com.xbongbong.paas.pojo.vo.DelColumnSetListVO;
import com.xbongbong.paas.pojo.vo.MobileFocusModeGetVO;
import com.xbongbong.paas.pojo.vo.MobileMenuModeSetGetVO;
import com.xbongbong.paas.pojo.vo.MobileModeGetVO;
import com.xbongbong.paas.pojo.vo.UpdateMobileFocusModeVO;
import com.xbongbong.paas.pojo.vo.UpdateMobileModeVO;
import com.xbongbong.paas.pojo.vo.UserConfigAddVO;
import com.xbongbong.paas.service.UserConfigService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.pro.config.pojo.dto.UserConfigAddBatchDTO;
import com.xbongbong.pro.config.pojo.dto.UserConfigSaveBatchDTO;
import com.xbongbong.pro.config.pojo.vo.UserConfigAddBatchVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.help.UserConfigHelp;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.CustomerStatementEnum;
import com.xbongbong.saas.enums.GuaranteeExplainConstant;
import com.xbongbong.saas.enums.ListGroupEnum;
import com.xbongbong.saas.enums.ListOptionEnum;
import com.xbongbong.saas.enums.OperateModuleTypeEnum;
import com.xbongbong.saas.enums.OperateTypeEnum;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.base.CommunicateBaseEnum;
import com.xbongbong.saas.enums.business.BatchFlowBillEnum;
import com.xbongbong.saas.enums.business.CostAdjustEnum;
import com.xbongbong.saas.enums.business.ExpenseEnum;
import com.xbongbong.saas.enums.business.OfficePreViewUrlEnum;
import com.xbongbong.saas.enums.business.ProductEnum;
import com.xbongbong.saas.enums.business.ProductSerialBalanceEnum;
import com.xbongbong.saas.enums.business.ReimbursementFormEnum;
import com.xbongbong.saas.enums.business.StockFlowBillEnum;
import com.xbongbong.saas.toolbox.util.UserConfigUtil;
import com.xbongbong.sys.domain.entity.UserConfigEntity;
import com.xbongbong.sys.enums.UserConfigEnum;
import com.xbongbong.sys.model.UserConfigModel;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author: wufeng
 * @date: 2018/9/11 20:37
 * @desrcption:
 */
@Service("userConfigService")
public class UserConfigServiceImpl implements UserConfigService {

    private static final Logger LOG = LoggerFactory.getLogger(UserConfigServiceImpl.class);

    @Resource
    private UserConfigModel userConfigModel;
    @Resource
    private MongoLogHelp mongoLogHelp;
    @Resource
    private PaasFormExplainModel paasFormExplainModel;
    @Resource
    private CommonHelp commonHelp;
    @Resource
    private UserConfigHelp userConfigHelp;



    @Override
    public UserConfigAddVO save(UserConfigAddDTO userConfigAddDTO) throws XbbException {
        UserConfigAddVO userConfigAddVO = new UserConfigAddVO();
        UserConfigEntity entity = new UserConfigEntity();
        BeanUtil.copyProperties(userConfigAddDTO, entity);
        String logMemoConstant = I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_ADD_CONFIG);
        OperateTypeEnum operateTypeEnum = OperateTypeEnum.NEW;
        try {
            UserConfigEntity userConfigEntity = userConfigModel.getByUserId(userConfigAddDTO.getUserId(), userConfigAddDTO.getCorpid(), userConfigAddDTO.getConfigAlias());

            if (userConfigEntity == null || userConfigEntity.getId() == null) {
                userConfigModel.insert(entity);
            } else {
                entity.setId(userConfigEntity.getId());
                userConfigModel.update(entity);
                logMemoConstant = I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_UPDATE_CONFIG);
                operateTypeEnum = OperateTypeEnum.EDIT;
            }
            userConfigAddVO.setUserConfigId(entity.getId());
        } catch (Exception e) {
            LOG.error("UserConfigService 数据库更新出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        // 日志
        String userName = userConfigAddDTO.getLoginUserName();
        String memo = String.format(logMemoConstant, userName, entity.getConfigName());
        mongoLogHelp.buildLog(userConfigAddDTO.getCorpid(), userConfigAddDTO.getUserId(), userName, OperateModuleTypeEnum.PAAS_CONFIG,
                operateTypeEnum, entity.getId().toString(), entity.getConfigName(), memo, userConfigAddDTO.getHttpHeader());

        return userConfigAddVO;
    }


    @Override
    public UserConfigAddBatchVO saveBatch(UserConfigAddBatchDTO userConfigAddBatchDTO) throws XbbException {
        UserConfigAddBatchVO userConfigAddBatchVO = new UserConfigAddBatchVO();
        try {
            Map<String, Object> params = BeanUtil.convertBean2Map(userConfigAddBatchDTO, true);
            List<String> configAliasList = new ArrayList<>();
            List<UserConfigEntity> resultList = new ArrayList<>();
            userConfigAddBatchDTO.getUserConfigList().forEach((item)->{
                UserConfigEntity userConfigEntity = new UserConfigEntity();
                BeanUtil.copyProperties(item, userConfigEntity);
                userConfigEntity.setCorpid(userConfigAddBatchDTO.getCorpid());
                userConfigEntity.setUserId(userConfigAddBatchDTO.getUserId());
                resultList.add(userConfigEntity);
                configAliasList.add(item.getConfigAlias());
            });
            params.put("configAliasIn", configAliasList);
            params.put("userId", userConfigAddBatchDTO.getUserId());
            params.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
            List<UserConfigEntity> userConfigList = userConfigModel.list(params);
            if(Objects.isNull(userConfigList)){
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            UserConfigSaveBatchDTO userConfigSaveBatchDTO = new UserConfigSaveBatchDTO();
            BeanUtil.copyProperties(userConfigAddBatchDTO, userConfigSaveBatchDTO);
            userConfigSaveBatchDTO.setUserConfigList(userConfigList);
            userConfigSaveBatchDTO.setResultList(resultList);
            userConfigSaveBatchDTO.setConfigAliasList(configAliasList);
            insertOrUpdate(userConfigSaveBatchDTO);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e){
            LOG.error("UserConfigService.saveBatch批量保存数据库出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return userConfigAddBatchVO;
    }

    @Override
    public ColumnSetAddVO columnSetAdd(ColumnSetAddDTO columnSetAddDTO) throws XbbException {
        ColumnSetAddVO userConfigAddVO = new ColumnSetAddVO();
        if(Objects.equals(columnSetAddDTO.getSubBusinessType(), XbbRefTypeEnum.SMART_REPLENISHMENT.getCode())){
            String sortType = columnSetAddDTO.getSortType();
            String sortAttr = columnSetAddDTO.getSortAttr();
            if (Objects.isNull(sortType) && Objects.nonNull(sortAttr)){
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100002, "请设置排序规则");
            }
        }
        try {
            long now = DateUtil.getInt();
            if (Objects.equals(columnSetAddDTO.getBusinessType(), XbbRefTypeEnum.PRODUCT.getCode()) && Objects.equals(columnSetAddDTO.getSubBusinessType(), XbbRefTypeEnum.STOCK_SEARCH.getCode())) {
                if (Objects.equals(ListGroupEnum.getByCode(columnSetAddDTO.getFormId().intValue()).getRefType(), XbbRefTypeEnum.GUARANTEE_SEARCH.getCode())) {
                    columnSetAddDTO.setGroupId(columnSetAddDTO.getFormId());
                } else if (Objects.equals(ListGroupEnum.getByCode(columnSetAddDTO.getFormId().intValue()).getRefType(), XbbRefTypeEnum.WARNING_SEARCH.getCode())) {
                    columnSetAddDTO.setGroupId(columnSetAddDTO.getFormId());
                }
            }
            String key = getKey(columnSetAddDTO.getSubBusinessType(),columnSetAddDTO.getAppId(),columnSetAddDTO.getFormId());
            String defaultSortConfigPrefix = getDefaultSortConfigPrefix(columnSetAddDTO.getBusinessType(),columnSetAddDTO.getSubBusinessType(),columnSetAddDTO.getGroupId());
            String defaultSortConfigAlias = defaultSortConfigPrefix + (key.isEmpty() ? "" : "_" + key);
            String columnInvisibleConfigPrefix = getColumnInvisibleConfigPrefix(columnSetAddDTO.getBusinessType(),columnSetAddDTO.getSubBusinessType(),columnSetAddDTO.getGroupId());
            String columnInvisibleConfigAlias = columnInvisibleConfigPrefix + (key.isEmpty() ? "" : "_" + key);
            String columnSortConfigPrefix = getColumnSortConfigPrefix(columnSetAddDTO.getBusinessType(),columnSetAddDTO.getSubBusinessType(),columnSetAddDTO.getGroupId());
            String columnSortConfigAlias = columnSortConfigPrefix + (key.isEmpty() ? "" : "_" + key);
            String columnFixedConfigPrefix = getColumnFixedConfigPrefix(columnSetAddDTO.getBusinessType(),columnSetAddDTO.getSubBusinessType(),columnSetAddDTO.getGroupId());
            String columnFixedConfigAlias = columnFixedConfigPrefix + (key.isEmpty() ? "" : "_" + key);
            Map<String, Object> params = BeanUtil.convertBean2Map(columnSetAddDTO, true);
            List<String> configAliasList = new ArrayList<>();
            configAliasList.add(defaultSortConfigAlias);
            configAliasList.add(columnInvisibleConfigAlias);
            configAliasList.add(columnSortConfigAlias);
            configAliasList.add(columnFixedConfigAlias);
            params.put("configAliasIn", configAliasList);
            params.put("userId", columnSetAddDTO.getUserId());
            params.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
            List<UserConfigEntity> userConfigList = userConfigModel.list(params);
            if(Objects.isNull(userConfigList)){
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            UserConfigEntity defaultSortEntity = new UserConfigEntity();
            BeanUtil.copyProperties(columnSetAddDTO, defaultSortEntity);
            defaultSortEntity.setConfigName(UserConfigEnum.DEFAULT_SORT_FIELD.getName());
            defaultSortEntity.setConfigAlias(defaultSortConfigAlias);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("sortAttr", columnSetAddDTO.getSortAttr());
            jsonObject.put("sortType", columnSetAddDTO.getSortType());
            defaultSortEntity.setConfigValue(JSONObject.toJSONString(jsonObject));
            defaultSortEntity.setAddTime(now);
            defaultSortEntity.setUpdateTime(now);
            defaultSortEntity.setUserId(columnSetAddDTO.getUserId());
            UserConfigEntity columnInvisibleEntity = new UserConfigEntity();
            BeanUtil.copyProperties(columnSetAddDTO, columnInvisibleEntity);
            columnInvisibleEntity.setConfigName(UserConfigEnum.COLUMN_INVISIBLE_FIELD.getName());
            columnInvisibleEntity.setConfigAlias(columnInvisibleConfigAlias);
            columnInvisibleEntity.setConfigValue(JSONArray.toJSONString(columnSetAddDTO.getInvisibleList()));
            columnInvisibleEntity.setAddTime(now);
            columnInvisibleEntity.setUpdateTime(now);
            columnInvisibleEntity.setUserId(columnSetAddDTO.getUserId());
            UserConfigEntity columnSortEntity = new UserConfigEntity();
            BeanUtil.copyProperties(columnSetAddDTO, columnSortEntity);
            columnSortEntity.setConfigName(UserConfigEnum.COLUMN_SORT_FIELD.getName());
            columnSortEntity.setConfigAlias(columnSortConfigAlias);
            columnSortEntity.setConfigValue(JSONArray.toJSONString(columnSetAddDTO.getColumnSort()));
            columnSortEntity.setAddTime(now);
            columnSortEntity.setUpdateTime(now);
            columnSortEntity.setUserId(columnSetAddDTO.getUserId());

            //表单冻结
            UserConfigEntity columnFixedEntity = new UserConfigEntity();
            BeanUtil.copyProperties(columnSetAddDTO, columnFixedEntity);
            columnFixedEntity.setConfigName(UserConfigEnum.COLUMN_FIXED_FIELD.getName());
            columnFixedEntity.setConfigAlias(columnFixedConfigAlias);
            columnFixedEntity.setConfigValue(Objects.isNull(columnSetAddDTO.getColumnFixedField()) ? "0" : columnSetAddDTO.getColumnFixedField().toString());
            columnFixedEntity.setAddTime(now);
            columnFixedEntity.setUpdateTime(now);
            columnFixedEntity.setUserId(columnSetAddDTO.getUserId());

            UserConfigSaveBatchDTO userConfigSaveBatchDTO = new UserConfigSaveBatchDTO();
            BeanUtil.copyProperties(columnSetAddDTO, userConfigSaveBatchDTO);
            userConfigSaveBatchDTO.setUserConfigList(userConfigList);
            List<UserConfigEntity> resultList = new ArrayList<>();
            resultList.add(defaultSortEntity);
            resultList.add(columnInvisibleEntity);
            resultList.add(columnSortEntity);
            resultList.add(columnFixedEntity);
            userConfigSaveBatchDTO.setResultList(resultList);
            userConfigSaveBatchDTO.setConfigAliasList(configAliasList);
            insertOrUpdate(userConfigSaveBatchDTO);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("UserConfigService 数据库更新列表设置出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return userConfigAddVO;
    }

    @Override
    public ColumnSetListVO columnSetList(ColumnSetListDTO columnSetListDTO) throws XbbException {
        ColumnSetListVO columnSetListVO = new ColumnSetListVO();
        try {
            String key = getKey(columnSetListDTO.getSubBusinessType(),columnSetListDTO.getAppId(),columnSetListDTO.getFormId());
            String defaultSortConfigPrefix = getDefaultSortConfigPrefix(columnSetListDTO.getBusinessType(),columnSetListDTO.getSubBusinessType(),columnSetListDTO.getGroupId());
            String defaultSortConfigAlias = defaultSortConfigPrefix + (key.isEmpty() ? "" : "_" + key);
            String columnInvisibleConfigPrefix = getColumnInvisibleConfigPrefix(columnSetListDTO.getBusinessType(),columnSetListDTO.getSubBusinessType(),columnSetListDTO.getGroupId());
            String columnInvisibleConfigAlias = columnInvisibleConfigPrefix + (key.isEmpty() ? "" : "_" + key);
            String columnSortConfigPrefix = getColumnSortConfigPrefix(columnSetListDTO.getBusinessType(),columnSetListDTO.getSubBusinessType(),columnSetListDTO.getGroupId());
            String columnSortConfigAlias = columnSortConfigPrefix + (key.isEmpty() ? "" : "_" + key);
            String columnFixedConfigPrefix = getColumnFixedConfigPrefix(columnSetListDTO.getBusinessType(),columnSetListDTO.getSubBusinessType(),columnSetListDTO.getGroupId());
            String columnFixedConfigAlias = columnFixedConfigPrefix + (key.isEmpty() ? "" : "_" + key);
            Map<String, Object> params = BeanUtil.convertBean2Map(columnSetListDTO, true);
            List<String> configAliasList = new ArrayList<>();
            configAliasList.add(defaultSortConfigAlias);
            configAliasList.add(columnInvisibleConfigAlias);
            configAliasList.add(columnSortConfigAlias);
            configAliasList.add(columnFixedConfigAlias);
            params.put("configAliasIn", configAliasList);
            params.put("userId", columnSetListDTO.getUserId());
            params.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
            List<UserConfigEntity> userConfigList = userConfigModel.list(params);
            Map<String, Object> userConfigMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if(userConfigList != null && !userConfigList.isEmpty()) {
                userConfigList.forEach((item)->{
                    if(item.getConfigAlias().equals(defaultSortConfigAlias)) {
                        userConfigMap.put(UserConfigEnum.DEFAULT_SORT_FIELD.getAlias(), JSONObject.parseObject(item.getConfigValue()));
                    } else if (item.getConfigAlias().equals(columnInvisibleConfigAlias)){
                        userConfigMap.put(UserConfigEnum.COLUMN_INVISIBLE_FIELD.getAlias(), JSONObject.parseArray(item.getConfigValue()));
                    } else if (item.getConfigAlias().equals(columnSortConfigAlias)){
                        userConfigMap.put(UserConfigEnum.COLUMN_SORT_FIELD.getAlias(), JSONObject.parseArray(item.getConfigValue()));
                    } else if (item.getConfigAlias().equals(columnFixedConfigAlias)) {
                        userConfigMap.put(UserConfigEnum.COLUMN_FIXED_FIELD.getAlias(), Integer.parseInt(item.getConfigValue()));
                    }
                });
            }else {
                if (!Objects.isNull(columnSetListDTO.getSubBusinessType())){
                    JSONObject item = new JSONObject();
                    String sortAttr;
                    item.put("sortType",StringConstant.SORT_DESC_TAG);
                    if (Objects.equals(columnSetListDTO.getBusinessType(), XbbRefTypeEnum.CUSTOMER_COMMUNICATE.getCode()) || Objects.equals(columnSetListDTO.getBusinessType(), XbbRefTypeEnum.SUPPLIER_COMMUNICATE.getCode())) {
                        // 跟进记录和供应商跟进记录的列表排序的默认是按照拜访时间倒序
                        sortAttr = CommunicateBaseEnum.COMMUNICATE_TIME.getAttr();
                    } else if (Objects.equals(columnSetListDTO.getBusinessType(), XbbRefTypeEnum.REIMBURSEMENT_FORM.getCode())) {
                        sortAttr = ReimbursementFormEnum.SUBMIT_TIME.getAttr();
                    }  else if (Objects.equals(columnSetListDTO.getBusinessType(), XbbRefTypeEnum.EXPENSE.getCode())) {
                        sortAttr = ExpenseEnum.CONSUME_TIME.getAttr();
                    } else if (Objects.equals(columnSetListDTO.getBusinessType(), XbbRefTypeEnum.STOCK_FLOW_BILL.getCode())) {
                        sortAttr = StockFlowBillEnum.TIME.getAttr();
                        item.put("sortType",StringConstant.SORT_ASC_TAG);
                    } else if (Objects.equals(columnSetListDTO.getBusinessType(), XbbRefTypeEnum.BATCH_FLOW_BILL.getCode())) {
                        sortAttr = BatchFlowBillEnum.TIME.getAttr();
                        item.put("sortType",StringConstant.SORT_ASC_TAG);
                    } else if (Objects.equals(columnSetListDTO.getBusinessType(), XbbRefTypeEnum.SEQ_SEARCH.getCode())) {
                        sortAttr = ProductSerialBalanceEnum.TIME.getAttr();
                        item.put("sortType",StringConstant.SORT_DESC_TAG);
                    } else if (Objects.equals(columnSetListDTO.getBusinessType(), XbbRefTypeEnum.CUSTOMER_STATEMENT.getCode())) {
                        sortAttr = CustomerStatementEnum.CHECKING_DATE.getAttr();
                        item.put("sortType",StringConstant.SORT_DESC_TAG);
                    } else if (Objects.equals(columnSetListDTO.getBusinessType(), XbbRefTypeEnum.COST_ADJUST.getCode())) {
                        sortAttr = CostAdjustEnum.ADD_TIME.getAttr();
                        item.put("sortType",StringConstant.SORT_DESC_TAG);
                    } else {
                        sortAttr = FieldTypeEnum.UPDATETIME.getAlias();
                    }
                    item.put("sortAttr", sortAttr);
                    userConfigMap.put(UserConfigEnum.DEFAULT_SORT_FIELD.getAlias(), item);
                }
            }
            if (Objects.equals(columnSetListDTO.getSubBusinessType(),XbbRefTypeEnum.STOCK_SEARCH.getCode())
                    && Objects.equals(columnSetListDTO.getGroupId(),ListGroupEnum.WARNING_SEARCH.getCode())
                    && !userConfigMap.containsKey(UserConfigEnum.COLUMN_INVISIBLE_FIELD.getAlias())){
                List<String> showList = Arrays.asList(ProductEnum.NAME.getAttr(),ProductEnum.SERIALNO.getAttr(),ProductEnum.SPECIFICATION.getAttr(),ProductEnum.UNIT.getAttr(),ProductEnum.WAREHOUSE.getAttr(),"minimumStock","maximumStock","option_15");
                PaasFormExplainEntity paasFormExplainEntity = paasFormExplainModel.getByBusinessType(XbbRefTypeEnum.PRODUCT.getCode(),columnSetListDTO.getCorpid());
                List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(),FieldAttrEntity.class);
                List<String> noShowList = new ArrayList<>();
                for (FieldAttrEntity fieldAttrEntity : explainList) {
                    if (!showList.contains(fieldAttrEntity.getAttr())){
                        noShowList.add(fieldAttrEntity.getAttr());
                    }
                }
                userConfigMap.put(UserConfigEnum.COLUMN_INVISIBLE_FIELD.getAlias(),noShowList);
            } else if (Objects.equals(columnSetListDTO.getSubBusinessType(),XbbRefTypeEnum.STOCK_SEARCH.getCode())
                    && (Objects.isNull(columnSetListDTO.getGroupId()) || Objects.equals(columnSetListDTO.getGroupId(),ListGroupEnum.PRODUCT_SEARCH.getCode()))
                    && !userConfigMap.containsKey(UserConfigEnum.COLUMN_INVISIBLE_FIELD.getAlias())){
                List<String> showList = Arrays.asList(ProductEnum.NAME.getAttr(),ProductEnum.SERIALNO.getAttr(),ProductEnum.SPECIFICATION.getAttr(),ProductEnum.UNIT.getAttr(),ProductEnum.PRODUCT_IMGS.getAttr(),ProductEnum.STOCK.getAttr(),ProductEnum.COST.getAttr());
                PaasFormExplainEntity paasFormExplainEntity = paasFormExplainModel.getByBusinessType(XbbRefTypeEnum.PRODUCT.getCode(),columnSetListDTO.getCorpid());
                List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(),FieldAttrEntity.class);
                List<String> noShowList = new ArrayList<>();
                for (FieldAttrEntity fieldAttrEntity : explainList) {
                    if (!showList.contains(fieldAttrEntity.getAttr())){
                        noShowList.add(fieldAttrEntity.getAttr());
                    }
                }
                userConfigMap.put(UserConfigEnum.COLUMN_INVISIBLE_FIELD.getAlias(),noShowList);
            } else if (Objects.equals(columnSetListDTO.getSubBusinessType(), XbbRefTypeEnum.STOCK_SEARCH.getCode())
                    && (Objects.isNull(columnSetListDTO.getFormId()) || Objects.equals(columnSetListDTO.getFormId().intValue(), ListGroupEnum.GUARANTEE_SEARCH.getCode()))) {
                // 批次保质期列表默认展示字段
                if (Objects.isNull(userConfigMap.get(UserConfigEnum.COLUMN_INVISIBLE_FIELD.getAlias()))) {
                    List<String> showList = GuaranteeExplainConstant.GUARANTEE_EXPLAIN;
                    PaasFormExplainEntity paasFormExplainEntity = paasFormExplainModel.getByBusinessType(XbbRefTypeEnum.PRODUCT.getCode(), columnSetListDTO.getCorpid());
                    List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
                    List<String> noShowList = new ArrayList<>();
                    for (FieldAttrEntity fieldAttrEntity : explainList) {
                        if (!showList.contains(fieldAttrEntity.getAttr())) {
                            noShowList.add(fieldAttrEntity.getAttr());
                        }
                    }
                    userConfigMap.put(UserConfigEnum.COLUMN_INVISIBLE_FIELD.getAlias(), noShowList);
                }
                JSONObject defaultSortField = (JSONObject) userConfigMap.get(UserConfigEnum.DEFAULT_SORT_FIELD.getAlias());
                if (BasicConstant.ZERO.equals(defaultSortField.size()) || Objects.equals(defaultSortField.getString("sortAttr"), ParameterConstant.ADD_TIME) || Objects.equals(defaultSortField.getString("sortAttr"), StringConstant.UPDATE_TIME)) {
                    defaultSortField.put("sortAttr", SelectProductEnum.EXPIRE_DATE.getSaasAttr());
                    defaultSortField.put("sortType", StringConstant.SORT_ASC_TAG);
                }
            }
            //如果没有设置冻结列，默认不冻结
            if (!userConfigMap.containsKey(UserConfigEnum.COLUMN_FIXED_FIELD.getAlias())) {
                userConfigMap.put(UserConfigEnum.COLUMN_FIXED_FIELD.getAlias(), 0);
            }
            columnSetListVO.setUserConfigMap(userConfigMap);
        } catch (Exception e) {
            LOG.error("UserConfigService 数据库获取列表设置出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return columnSetListVO;
    }

    protected void setOption_3(JSONArray jsonArray) {
        if (Objects.nonNull(jsonArray)){
            Boolean isOption_3=true;
            for (int i=0;i<jsonArray.size();i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                if (Objects.nonNull(jsonObject)){
                    String attr = jsonObject.getString("attr");
                    if (Objects.equals(attr, ListOptionEnum.CALL.getAttr())){
                        isOption_3 =false;
                        break;
                    }
                }
            }
            if (isOption_3){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("sort",-1);
                jsonObject.put("attr",ListOptionEnum.CALL.getAttr());
                jsonArray.add(0,jsonObject);
            }
        }
    }

    private String getKey(Integer subBusinessType,Long appId,Long formId) {
        XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(subBusinessType);
        switch (xbbRefTypeEnum){
            case STOCK_SEARCH:
            case WAIT_PURCHASE:
            case WAIT_PRODUCE:
            case WAIT_OUTSTOCK:
                return "";
            default:
                return appId + "_" + formId;
        }
    }
    @Override
    public String getColumnInvisibleConfigPrefix(Integer businessType, Integer subBusinessType, Long groupId) {
        XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(subBusinessType);
        switch (xbbRefTypeEnum){
            case STOCK_SEARCH:
                if (Objects.isNull(groupId)){
                    return UserConfigEnum.STOCK_COLUMN_INVISIBLE_FIELD.getAlias();
                } else if (Objects.equals(groupId.intValue(), ListGroupEnum.WARNING_SEARCH.getCode())){
                    return UserConfigEnum.WARN_COLUMN_INVISIBLE_FIELD.getAlias();
                }else if (Objects.equals(groupId.intValue(),ListGroupEnum.GUARANTEE_SEARCH.getCode())){
                    return UserConfigEnum.GUARANTEE_PERIOD_COLUMN_INVISIBLE_FIELD.getAlias();
                }
                return UserConfigEnum.STOCK_COLUMN_INVISIBLE_FIELD.getAlias();
            case WARNING_SEARCH:
                return UserConfigEnum.WARN_COLUMN_INVISIBLE_FIELD.getAlias();
            case GUARANTEE_SEARCH:
                return UserConfigEnum.GUARANTEE_PERIOD_COLUMN_INVISIBLE_FIELD.getAlias();
            case WAIT_PURCHASE:
                if (Objects.equals(businessType,XbbRefTypeEnum.CONTRACT.getCode())){
                    return "waitPurchaseContractInvisibleField";
                }else {
                    return "waitPurchaseProductInvisibleField";
                }
            case WAIT_PRODUCE:
                if (Objects.equals(businessType,XbbRefTypeEnum.CONTRACT.getCode())){
                    return "waitProduceContractInvisibleField";
                }else {
                    return "waitProduceProductInvisibleField";
                }
            case WAIT_OUTSTOCK:
                if (Objects.equals(businessType,XbbRefTypeEnum.CONTRACT.getCode())){
                    return "waitOutstockContractInvisibleField";
                }else {
                    return "waitOutstockProductInvisibleField";
                }
            case WAIT_INSTOCK_PURCHASE:
                if (Objects.equals(businessType,XbbRefTypeEnum.PURCHASE.getCode())){
                    return "waitInstockPurchaseInvisibleField";
                }else if (Objects.equals(businessType,XbbRefTypeEnum.PRODUCT.getCode())){
                    return "waitInstockProductInvisibleField";
                }
            default:
                return UserConfigEnum.COLUMN_INVISIBLE_FIELD.getAlias();
        }
    }

    @Override
    public String getColumnSortConfigPrefix(Integer businessType, Integer subBusinessType, Long groupId) {
        XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(subBusinessType);
        switch (xbbRefTypeEnum){
            case STOCK_SEARCH:
                if (Objects.isNull(groupId) ){
                    return UserConfigEnum.STOCK_COLUMN_SORT_FIELD.getAlias();
                } else if (Objects.equals(groupId.intValue(), ListGroupEnum.WARNING_SEARCH.getCode())){
                    return UserConfigEnum.WARN_COLUMN_SORT_FIELD.getAlias();
                }else if (Objects.equals(groupId.intValue(),ListGroupEnum.GUARANTEE_SEARCH.getCode())){
                    return UserConfigEnum.GUARANTEE_PERIOD_COLUMN_SORT_FIELD.getAlias();
                }
                return UserConfigEnum.STOCK_COLUMN_SORT_FIELD.getAlias();
            case GUARANTEE_SEARCH:
                return UserConfigEnum.GUARANTEE_PERIOD_COLUMN_SORT_FIELD.getAlias();
            case WARNING_SEARCH:
                return UserConfigEnum.WARN_COLUMN_SORT_FIELD.getAlias();
            case WAIT_OUTSTOCK:
                if (Objects.equals(businessType,XbbRefTypeEnum.CONTRACT.getCode())){
                    return "waitOutstockContractColumnSortField";
                }else {
                    return "waitOutstockProductColumnSortField";
                }
            case WAIT_INSTOCK_PURCHASE:
                if (Objects.equals(businessType,XbbRefTypeEnum.PURCHASE.getCode())){
                    return "waitInstockPurchaseColumnSortField";
                }else if (Objects.equals(businessType,XbbRefTypeEnum.PRODUCT.getCode())){
                    return "waitInstockProductColumnSortField";
                }
            case WAIT_PURCHASE:
                if (Objects.equals(businessType,XbbRefTypeEnum.CONTRACT.getCode())){
                    return "waitPurchaseContractColumnSortField";
                }else if (Objects.equals(businessType,XbbRefTypeEnum.PRODUCT.getCode())){
                    return "waitPurchaseProductColumnSortField";
                }
            case SMART_REPLENISHMENT:
                return "smartReplenishmentColumnSortField";
            default:
                return UserConfigEnum.COLUMN_SORT_FIELD.getAlias();
        }
    }

    /**
     * 设置表单冻结查询条件前缀（存在特殊逻辑，根据后缀key无法判断当前表单）
     * @param businessType 业务类型
     * @param subBusinessType 子业务类型
     * @param groupId 分组id
     * @author 李少杰
     * @return 查询条件前缀
     */
    @Override
    public String getColumnFixedConfigPrefix(Integer businessType, Integer subBusinessType, Long groupId) {
        XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(subBusinessType);
        switch (xbbRefTypeEnum){
            case STOCK_SEARCH:
                if (Objects.isNull(groupId)){
                    return UserConfigEnum.STOCK_COLUMN_FIXED_FIELD.getAlias();
                } else if (Objects.equals(groupId.intValue(), ListGroupEnum.WARNING_SEARCH.getCode())){
                    return UserConfigEnum.WARN_COLUMN_FIXED_FIELD.getAlias();
                }else if (Objects.equals(groupId.intValue(),ListGroupEnum.GUARANTEE_SEARCH.getCode())){
                    return UserConfigEnum.GUARANTEE_PERIOD_COLUMN_FIXED_FIELD.getAlias();
                }
                return UserConfigEnum.STOCK_COLUMN_FIXED_FIELD.getAlias();
            case WARNING_SEARCH:
                return UserConfigEnum.WARN_COLUMN_FIXED_FIELD.getAlias();
            case GUARANTEE_SEARCH:
                return UserConfigEnum.GUARANTEE_PERIOD_COLUMN_FIXED_FIELD.getAlias();
            case WAIT_PURCHASE:
                if (Objects.equals(businessType,XbbRefTypeEnum.CONTRACT.getCode())){
                    return "waitPurchaseContractFixedField";
                }else {
                    return "waitPurchaseProductFixedField";
                }
            case WAIT_PRODUCE:
                if (Objects.equals(businessType,XbbRefTypeEnum.CONTRACT.getCode())){
                    return "waitProduceContractFixedField";
                }else {
                    return "waitProduceProductFixedField";
                }
            case WAIT_OUTSTOCK:
                if (Objects.equals(businessType,XbbRefTypeEnum.CONTRACT.getCode())){
                    return "waitOutstockContractFixedField";
                }else {
                    return "waitOutstockProductFixedField";
                }
            case WAIT_INSTOCK_PURCHASE:
                if (Objects.equals(businessType,XbbRefTypeEnum.PURCHASE.getCode())){
                    return "waitInstockPurchaseFixedField";
                }else if (Objects.equals(businessType,XbbRefTypeEnum.PRODUCT.getCode())){
                    return "waitInstockProductFixedField";
                }
            default:
                return UserConfigEnum.COLUMN_FIXED_FIELD.getAlias();
        }
    }

    @Override
    public String getDefaultSortConfigPrefix(Integer businessType, Integer subBusinessType, Long groupId) {
        return UserConfigUtil.getDefaultSortConfigPrefix(businessType, subBusinessType, groupId);
    }


    @Override
    public DelColumnSetAddVO delColumnSetAdd(DelColumnSetAddDTO delColumnSetAddDTO) throws XbbException {
        DelColumnSetAddVO delColumnSetAddVO = new DelColumnSetAddVO();

        try {
            long now = DateUtil.getInt();

            String defaultSortConfigAlias = UserConfigEnum.DEFAULT_SORT_FIELD_DEL.getAlias() + "_" + delColumnSetAddDTO.getAppId() + "_" + delColumnSetAddDTO.getFormId();
            String columnInvisibleConfigAlias = UserConfigEnum.COLUMN_INVISIBLE_FIELD_DEL.getAlias() + "_" + delColumnSetAddDTO.getAppId() + "_" + delColumnSetAddDTO.getFormId();
            Map<String, Object> params = BeanUtil.convertBean2Map(delColumnSetAddDTO, true);
            List<String> configAliasList = new ArrayList<>();
            configAliasList.add(defaultSortConfigAlias);
            configAliasList.add(columnInvisibleConfigAlias);
            params.put("configAliasIn", configAliasList);
            params.put("userId", delColumnSetAddDTO.getUserId());
            params.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
            List<UserConfigEntity> userConfigList = userConfigModel.list(params);
            if(Objects.isNull(userConfigList)){
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            UserConfigEntity defaultSortEntity = new UserConfigEntity();
            BeanUtil.copyProperties(delColumnSetAddDTO, defaultSortEntity);
            defaultSortEntity.setConfigName(UserConfigEnum.DEFAULT_SORT_FIELD_DEL.getName());
            defaultSortEntity.setConfigAlias(defaultSortConfigAlias);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("sortAttr", delColumnSetAddDTO.getSortAttr());
            jsonObject.put("sortType", delColumnSetAddDTO.getSortType());
            defaultSortEntity.setConfigValue(JSONObject.toJSONString(jsonObject));
            defaultSortEntity.setAddTime(now);
            defaultSortEntity.setUpdateTime(now);
            defaultSortEntity.setUserId(delColumnSetAddDTO.getUserId());
            UserConfigEntity columnInvisibleEntity = new UserConfigEntity();
            BeanUtil.copyProperties(delColumnSetAddDTO, columnInvisibleEntity);
            columnInvisibleEntity.setConfigName(UserConfigEnum.COLUMN_INVISIBLE_FIELD_DEL.getName());
            columnInvisibleEntity.setConfigAlias(columnInvisibleConfigAlias);
            columnInvisibleEntity.setConfigValue(JSONArray.toJSONString(delColumnSetAddDTO.getInvisibleList()));
            columnInvisibleEntity.setAddTime(now);
            columnInvisibleEntity.setUpdateTime(now);
            columnInvisibleEntity.setUserId(delColumnSetAddDTO.getUserId());

            UserConfigSaveBatchDTO userConfigSaveBatchDTO = new UserConfigSaveBatchDTO();
            BeanUtil.copyProperties(delColumnSetAddDTO, userConfigSaveBatchDTO);
            userConfigSaveBatchDTO.setUserConfigList(userConfigList);
            List<UserConfigEntity> resultList = new ArrayList<>();
            resultList.add(defaultSortEntity);
            resultList.add(columnInvisibleEntity);
            userConfigSaveBatchDTO.setResultList(resultList);
            userConfigSaveBatchDTO.setConfigAliasList(configAliasList);
            insertOrUpdate(userConfigSaveBatchDTO);
        } catch (XbbException e) {
            throw e;
        }  catch (Exception e) {
            LOG.error("UserConfigService 数据库更新回收站的列表设置出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return delColumnSetAddVO;
    }


    public void insertOrUpdate(UserConfigSaveBatchDTO userConfigSaveBatchDTO) throws XbbException{
        try {
            List<UserConfigEntity> userConfigList = userConfigSaveBatchDTO.getUserConfigList();
            List<UserConfigEntity> resultList = userConfigSaveBatchDTO.getResultList();
            List<String> configAliasList = userConfigSaveBatchDTO.getConfigAliasList();
            String corpid = userConfigSaveBatchDTO.getCorpid();
            String userId = userConfigSaveBatchDTO.getUserId();
            List<UserConfigEntity> insertUserConfigList = new ArrayList<>();
            List<UserConfigEntity> updateUserConfigList = new ArrayList<>();
            Map<String, UserConfigEntity> userConfigMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if(userConfigList != null && !userConfigList.isEmpty()) {
                userConfigList.forEach((item)->{
                    userConfigMap.put(item.getConfigAlias(), item);
                });
            }
            if (userConfigList == null || userConfigList.isEmpty()) {
                for(UserConfigEntity item: resultList){
                    long now = DateUtil.getInt();
                    item.setAddTime(now);
                    item.setUpdateTime(now);
                    insertUserConfigList.add(item);
                }
            } else if(configAliasList.size() != userConfigList.size()) {
                for (UserConfigEntity item : resultList) {
                    if (userConfigMap.containsKey(item.getConfigAlias())) {
                        long now = DateUtil.getInt();
                        item.setId(userConfigMap.get(item.getConfigAlias()).getId());
                        item.setUpdateTime(now);
                        updateUserConfigList.add(item);
                    } else {
                        long now = DateUtil.getInt();
                        item.setAddTime(now);
                        item.setUpdateTime(now);
                        insertUserConfigList.add(item);
                    }
                }
            } else {
                resultList.forEach((item)->{
                    long now = DateUtil.getInt();
                    item.setId(userConfigMap.get(item.getConfigAlias()).getId());
                    item.setUpdateTime(now);
                    updateUserConfigList.add(item);
                });
            }
            String userName = userConfigSaveBatchDTO.getLoginUserName();
            List<String> name = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if(!insertUserConfigList.isEmpty()){
                userConfigModel.insertBatch(insertUserConfigList);

                // 日志
                insertUserConfigList.forEach(insertUserConfig -> name.add(insertUserConfig.getConfigName()));
                String nameStr = StringUtils.join(name, "，");
                if(insertUserConfigList.size() > 1){
                    String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_BATCH_ADD_CONFIG), userName, insertUserConfigList.size());
                    List<InfoArrPojo> infoArrPojoList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    InfoArrPojo infoArrPojo = new InfoArrPojo();
                    infoArrPojo.setTitle(OperateModuleTypeEnum.PAAS_CONFIG.getName());
                    infoArrPojo.setContent(nameStr);
                    infoArrPojoList.add(infoArrPojo);
                    mongoLogHelp.buildLog4InfoArr(infoArrPojoList, corpid, userId, userName, OperateModuleTypeEnum.PAAS_CONFIG, OperateTypeEnum.NEW,
                            "", "", memo, userConfigSaveBatchDTO.getHttpHeader());
                } else if (insertUserConfigList.size() == 1) {
                    String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_ADD_CONFIG), userName, name.get(0));
                    mongoLogHelp.buildLog(corpid, userId, userName, OperateModuleTypeEnum.PAAS_CONFIG, OperateTypeEnum.NEW,
                            insertUserConfigList.get(0).getId().toString(), nameStr, memo, userConfigSaveBatchDTO.getHttpHeader());
                }
            } else {
                userConfigModel.updateBatch(updateUserConfigList, corpid);

                // 日志
                updateUserConfigList.forEach(updateUserConfig -> name.add(updateUserConfig.getConfigName()));
                String nameStr = StringUtils.join(name, "，");
                if(insertUserConfigList.size() > 1){
                    String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_BATCH_UPDATE_CONFIG), userName, insertUserConfigList.size());
                    List<InfoArrPojo> infoArrPojoList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    InfoArrPojo infoArrPojo = new InfoArrPojo();
                    infoArrPojo.setTitle(OperateModuleTypeEnum.PAAS_CONFIG.getName());
                    infoArrPojo.setContent(nameStr);
                    infoArrPojoList.add(infoArrPojo);
                    mongoLogHelp.buildLog4InfoArr(infoArrPojoList, corpid, userId, userName, OperateModuleTypeEnum.PAAS_CONFIG, OperateTypeEnum.EDIT,
                            "", "", memo, userConfigSaveBatchDTO.getHttpHeader());
                } else if (insertUserConfigList.size() == 1) {
                    String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_UPDATE_CONFIG), userName, name.get(0));
                    mongoLogHelp.buildLog(corpid, userId, userName, OperateModuleTypeEnum.PAAS_CONFIG, OperateTypeEnum.EDIT,
                            insertUserConfigList.get(0).getId().toString(), nameStr, memo, userConfigSaveBatchDTO.getHttpHeader());
                }
            }

        }catch (Exception e) {
            LOG.error("UserConfigService 数据库更新默认排序和列表显示配置出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
    }


    @Override
    public DelColumnSetListVO delColumnSetList(DelColumnSetListDTO delColumnSetListDTO) throws XbbException {
        DelColumnSetListVO delColumnSetListVO = new DelColumnSetListVO();
        try {
            String defaultSortConfigAlias = UserConfigEnum.DEFAULT_SORT_FIELD_DEL.getAlias() + "_" + delColumnSetListDTO.getAppId() + "_" + delColumnSetListDTO.getFormId();

            String columnInvisibleConfigAlias = UserConfigEnum.COLUMN_INVISIBLE_FIELD_DEL.getAlias() + "_" + delColumnSetListDTO.getAppId() + "_" + delColumnSetListDTO.getFormId();
            Map<String, Object> params = BeanUtil.convertBean2Map(delColumnSetListDTO, true);
            List<String> configAliasList = new ArrayList<>();
            configAliasList.add(defaultSortConfigAlias);
            configAliasList.add(columnInvisibleConfigAlias);
            params.put("configAliasIn", configAliasList);
            List<UserConfigEntity> userConfigList = userConfigModel.list(params);
            Map<String, Object> userConfigMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if(userConfigList != null && !userConfigList.isEmpty()) {
                userConfigList.forEach((item)->{
                    if(item.getConfigName().equals(defaultSortConfigAlias)) {
                        userConfigMap.put(UserConfigEnum.DEFAULT_SORT_FIELD_DEL.getAlias(), JSONObject.parseObject(item.getConfigAlias()));
                    }else {
                        userConfigMap.put(UserConfigEnum.COLUMN_INVISIBLE_FIELD_DEL.getAlias(), JSONObject.parseArray(item.getConfigAlias()));
                    }
                });
            }
            delColumnSetListVO.setUserConfigMap(userConfigMap);
        } catch (Exception e) {
            LOG.error("UserConfigService 数据库获取列表设置出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return delColumnSetListVO;
    }

    @Override
    public boolean isFocus(BaseDTO baseDTO) throws XbbException {
        // 获取用户是否是专注模式
        UserConfigEntity userConfigEntity = userConfigModel.getByUserId(baseDTO.getUserId(),baseDTO.getCorpid(),UserConfigEnum.MOBILE_INDEX_MODE.getAlias());
        if (Objects.nonNull(userConfigEntity)) {
            if (Objects.equals(userConfigEntity.getConfigValue(), String.valueOf(MobileIndexModeEnum.CLASSIC_MODE.getCode()))){
                return false;
            }
        }else {
            // 用户是否有经典模式，有的话是默认不开启专注模式
            if (commonHelp.hasClassicMode(baseDTO.getCorpid())){
                return false;
            }
        }
        return true;
    }

    @Override
    public UpdateMobileModeVO updateMobileMode(UpdateMobileModeDTO updateMobileModeDTO) throws XbbException {
        UpdateMobileModeVO updateMobileModeVO = new UpdateMobileModeVO();
        MobileIndexModeEnum mobileModeTypeEnum = MobileIndexModeEnum.getByCode(updateMobileModeDTO.getMode());
        if (null == mobileModeTypeEnum){
            return updateMobileModeVO;
        }
        UserConfigEntity userConfigEntity = userConfigModel.getByUserId(updateMobileModeDTO.getUserId(),updateMobileModeDTO.getCorpid(),UserConfigEnum.MOBILE_INDEX_MODE.getAlias());
        if (Objects.nonNull(userConfigEntity)) {
            userConfigEntity.setConfigValue(String.valueOf(mobileModeTypeEnum.getCode()));
            userConfigModel.update(userConfigEntity);
        }else {
            userConfigEntity = new UserConfigEntity(updateMobileModeDTO.getCorpid(),updateMobileModeDTO.getUserId(),UserConfigEnum.MOBILE_INDEX_MODE.getName(),UserConfigEnum.MOBILE_INDEX_MODE.getAlias(),String.valueOf(mobileModeTypeEnum.getCode()));
            userConfigModel.insert(userConfigEntity);
        }
        return updateMobileModeVO;
    }

    @Override
    public MobileModeGetVO getMobileMode(BaseDTO baseDTO) throws XbbException {
        return userConfigHelp.getMobileMode(baseDTO);
    }

    @Override
    public UpdateMobileFocusModeVO updateMobileFocusMode(UpdateMobileFocusModeDTO updateMobileFocusModeDTO) throws XbbException {
        UpdateMobileFocusModeVO updateMobileFocusModeVO = new UpdateMobileFocusModeVO();
        UserConfigEntity userConfigEntity = userConfigModel.getByUserId(updateMobileFocusModeDTO.getUserId(),updateMobileFocusModeDTO.getCorpid(),UserConfigEnum.WORK_OR_CATEGORY_MODE.getAlias());
        MobileFocusModeTypeEnum mobileFocusModeTypeEnum = MobileFocusModeTypeEnum.getByCode(updateMobileFocusModeDTO.getMode());
        if (null == mobileFocusModeTypeEnum){
            return updateMobileFocusModeVO;
        }
        if (Objects.nonNull(userConfigEntity)) {
            userConfigEntity.setConfigValue(mobileFocusModeTypeEnum.getValue());
            userConfigModel.update(userConfigEntity);
        }else {
            userConfigEntity = new UserConfigEntity(updateMobileFocusModeDTO.getCorpid(),updateMobileFocusModeDTO.getUserId(),UserConfigEnum.WORK_OR_CATEGORY_MODE.getName(),UserConfigEnum.WORK_OR_CATEGORY_MODE.getAlias(),mobileFocusModeTypeEnum.getValue());
            userConfigModel.insert(userConfigEntity);
        }
        return updateMobileFocusModeVO;
    }

    @Override
    public MobileFocusModeGetVO getMobileFocusMode(BaseDTO baseDTO) throws XbbException {
        MobileFocusModeGetVO mobileFocusModeGetVO = new MobileFocusModeGetVO();
        UserConfigEntity userConfigEntity = userConfigModel.getByUserId(baseDTO.getUserId(),baseDTO.getCorpid(),UserConfigEnum.WORK_OR_CATEGORY_MODE.getAlias());
        if (null != userConfigEntity){
            MobileFocusModeTypeEnum mobileFocusModeTypeEnum = MobileFocusModeTypeEnum.getByValue(userConfigEntity.getConfigValue());
            if (null == mobileFocusModeTypeEnum){
                mobileFocusModeGetVO.setMode(MobileFocusModeTypeEnum.WORK.getCode());
            }else {
                mobileFocusModeGetVO.setMode(mobileFocusModeTypeEnum.getCode());
            }
        }else {
            mobileFocusModeGetVO.setMode(MobileFocusModeTypeEnum.WORK.getCode());
        }
        return mobileFocusModeGetVO;
    }

    @Override
    public MobileMenuModeSetGetVO getMobileMenuModeSet(BaseDTO baseDTO) throws XbbException {
        MobileMenuModeSetGetVO mobileMenuModeSetGetVO = new MobileMenuModeSetGetVO();
        // 移动端当前模式
        MobileModeGetVO mobileModeGetVO = getMobileMode(baseDTO);

        // 移动端默认看板
        MobileFocusModeGetVO mobileFocusModeGetVO = getMobileFocusMode(baseDTO);

        // 是否有经典模式
        Boolean hasClassicMode = commonHelp.hasClassicMode(baseDTO.getCorpid());


        mobileMenuModeSetGetVO.setCurMode(mobileModeGetVO.getMode());
        mobileMenuModeSetGetVO.setDefaultIndexBoard(mobileFocusModeGetVO.getMode());
        List<MobileMenuModeSetGetPojo> modeList = new ArrayList<>();
        for (MobileIndexModeEnum mobileIndexModeEnum : MobileIndexModeEnum.getAllEnum()) {
            if (!hasClassicMode && Objects.equals(mobileIndexModeEnum.getCode(), MobileIndexModeEnum.CLASSIC_MODE.getCode())) {
                continue;
            }
            MobileMenuModeSetGetPojo pojo = new MobileMenuModeSetGetPojo(mobileIndexModeEnum.getCode(), mobileIndexModeEnum.getAlias(), mobileIndexModeEnum.getName());
            modeList.add(pojo);
        }
        if (hasClassicMode){
            mobileMenuModeSetGetVO.setHasClassicMode(1);
        }else {
            mobileMenuModeSetGetVO.setHasClassicMode(0);
        }
        mobileMenuModeSetGetVO.setModeList(modeList);
        return mobileMenuModeSetGetVO;
    }



    @Override
    public String getOfficPreviewUrl(String corpid, String userId) throws XbbException {
        try{
            UserConfigEnum officPreviewUrl = UserConfigEnum.OFFIC_PREVIEW_URL;
            String alias = officPreviewUrl.getAlias();
            UserConfigEntity configEntity = userConfigModel.getByUserId(userId, corpid, alias);
            if(configEntity!=null){
                String configValue = configEntity.getConfigValue();
                String url = OfficePreViewUrlEnum.getUrlByCode(configValue);
                return url;
            }else{
                return OfficePreViewUrlEnum.getDefaultUrl();
            }
        }catch (Exception e){
            return OfficePreViewUrlEnum.getDefaultUrl();
        }
    }

    @Override
    public void saveOfficPreviewUrl(OfficPreviewUrlSaveDTO officPreviewUrlSaveDTO) throws XbbException {
        String corpid = officPreviewUrlSaveDTO.getCorpid();
        String userId = officPreviewUrlSaveDTO.getUserId();
        String url = officPreviewUrlSaveDTO.getUrl();
        String code = OfficePreViewUrlEnum.getCodeByUrl(url);
        UserConfigEnum officPreviewUrl = UserConfigEnum.OFFIC_PREVIEW_URL;
        String alias = officPreviewUrl.getAlias();
        String name = officPreviewUrl.getName();
        if(code!=null){
            userConfigModel.save(corpid, userId, alias, name, code);
        }
    }

    @Override
    public UserConfigAddVO openOnlyShowSpecifitions(UserConfigAddDTO userConfigAddDTO) throws XbbException {
        UserConfigAddVO userConfigAddVO = new UserConfigAddVO();
        String value = userConfigAddDTO.getConfigValue();
        if (StringUtils.isNotEmpty(value)) {
            userConfigAddDTO.setConfigAlias(UserConfigEnum.ONLY_SHOW_ENABLE_SPECIFITION.getAlias());
            userConfigAddDTO.setConfigName(UserConfigEnum.ONLY_SHOW_ENABLE_SPECIFITION.getName());
            return save(userConfigAddDTO);
        }
        return userConfigAddVO;
    }

    @Override
    public UserConfigEntity getOnlyShowSpecifitions(String corpid, String userId) {
        Map<String, Object> param = new HashMap<>();
        param.put("corpid",corpid);
        param.put("userId",userId);
        param.put("configAlias",UserConfigEnum.ONLY_SHOW_ENABLE_SPECIFITION.getAlias());
        param.put("del",DelEnum.NORMAL.getDel());
        List<UserConfigEntity> list = userConfigModel.list(param);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }


    public void setUserConfigList(List<UserConfigEntity> addUserConfigList, UserConfigEntity userConfigEntity, String configName, String ConfigAlias, String ConfigValue) {
        UserConfigEntity temp = new UserConfigEntity();
        BeanUtil.copyProperties(userConfigEntity, temp);
        temp.setConfigName(configName);
        temp.setConfigAlias(ConfigAlias);
        temp.setConfigValue(ConfigValue);
        addUserConfigList.add(temp);
    }

}
